package co.gov.medellin.sdep.sisdep.integration.outlook;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.activation.DataHandler;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.StringUtils;

/**
 * Clase que permite realizar envio de un email con un evento al MS Outlook, 
 * con el obejtivo de registrar una actividad en la agenda del funcionario.
 * 
 * @author carlos.perez@consult-soft.com
 * @version 08-06-2012 
 */
public class OutlookMeetingClient implements Serializable {
	private String outgoingMailServer;
	private String fromEmail;
	private String toEmail;
	private String subject;
	private String rsvpTo;
	private String organizer;
	private TimeZone timezone;
	private String location;
	private String message;
	
	/** Objetos para el servicio de envio de email:  */
	private JavaMailSender mailSender;
	

	/**
	 * Metodo que permite incializar las propiedades basicas para el envio del
	 * evento al MS Outlook via email.
	 *
	 * @author carlos.perez@consult-soft.com
	 * @version 08-06-2012 
	 * @throws Exception
	 */
	private void setupOutlookClient() throws Exception {
		setFromEmail("carlos.perez@consult-soft.com");
		setToEmail("carlo.perez@medellin.gov.co");
		setSubject("NUEVA AGENDA DE VISITA - SISDEP _0_");
		setRsvpTo("carlos.perez@consult-soft.com");
		setOrganizer("carlos.perez@consult-soft.com");
		setTimeZone(TimeZone.getDefault());
		setLocation("MEDELLIN-CIUDAD");
		setMessage("NUEVA AGENDA DE VISITA - SISDEP _0_");
	}

	/**
	 * Metodo que permite incializar por defecto las propiedades basicas para el envio del
	 * evento al MS Outlook via email. ESto para usar el Objeto LOCALmente en un entorno de DLLO.
	 *
	 * @author carlos.perez@consult-soft.com
	 * @version 08-06-2012 
	 * @throws Exception
	 */	
	private void setupOutlookClientDefault() {
		mailSender = null;
	}

	/**
	 * 
	 * @throws Exception
	 */
	public void send() throws Exception {
		if(mailSender != null) {
			try {
				MimeMessage message = mailSender.createMimeMessage();
				message.setFrom(new InternetAddress(fromEmail));
				message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
				message.addRecipient(Message.RecipientType.TO, new InternetAddress("william.restrepo@medellin.gov.co"));
				message.setSubject(subject);
	
				SimpleDateFormat timeStmpFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
				String body =
					"BEGIN:VCALENDAR\n"
						+ "PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n"
						+ "VERSION:2.0\n"
						+ "METHOD:REQUEST\n"
						+ "BEGIN:VEVENT\n"
						+ "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:"
						+ fromEmail
						+ "\n"
						+ "ORGANIZER:MAILTO:"
						+ fromEmail
						+ "\n"
						+ "DTSTART:20120625T140000Z\n"
						+ "DTEND:20120625T160000Z\n"
						+ "LOCATION:"
						+ location
						+ "\n"
						+ "TRANSP:OPAQUE\n"
						+ "SEQUENCE:0\n"
						+ "UID:" + (new Date().getTime()) + "\n"  // Completar un UID basado en la fecha+hora actual
						+ " 000004377FE5C37984842BF9440448399EB02\n"
						+ "DTSTAMP:"
						+ timeStmpFormat.format(Calendar.getInstance(TimeZone.getTimeZone("GMT-5")).getTime()) // uso horario bogota-colombia
						+ "\n"
						+ "CATEGORIES:Meeting\n"
						+ "DESCRIPTION:"
						+ StringUtils.replace(this.message, "\n", "\\n")
						+ "\n\n"
						+ "SUMMARY:"
						+ subject
						+ "\n"
						+ "PRIORITY:5\n"
						+ "CLASS:PUBLIC\n"
						+ "BEGIN:VALARM\n"
						+ "TRIGGER:-PT15M\n"
						+ "ACTION:DISPLAY\n"
						+ "DESCRIPTION:Reminder\n"
						+ "END:VALARM\n"
						+ "END:VEVENT\n"
						+ "END:VCALENDAR";
	
				//	   Fill the message
				DataHandler handler = new DataHandler(new OutlookMeetingDataSource(body));
				message.setDataHandler(handler);
				mailSender.send(message);	
				
			} catch (MessagingException me) {
				me.printStackTrace();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} // enf-if check mailSender			
	}

	public void setOutgoingMailServer(String outgoingMailServer) {
		this.outgoingMailServer = outgoingMailServer;
	}

	public void setFromEmail(String fromEmail) {
		this.fromEmail = fromEmail;
	}

	public void setToEmail(String toEmail) {
		this.toEmail = toEmail;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setRsvpTo(String rsvpTo) {
		this.rsvpTo = rsvpTo;
	}

	public void setOrganizer(String organizer) {
		this.organizer = organizer;
	}

	public void setTimeZone(TimeZone timezone) {
		this.timezone = timezone;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	/**
	* @return the mailSender
	*/
	public JavaMailSender getMailSender() {
		return mailSender;
	}

	/**
	* @param mailSender the mailSender to set
	*/
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}
	
}
